import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraApiAccessLogApi } from '#/api/infra/api-access-log';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { $t } from '#/locales';
import { JsonViewer } from '@vben/common-ui';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDateTime } from '@vben/utils';

import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'userId',
      label: $t('views.infra.apiAccessLog.userId'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.apiAccessLog.userIdPlaceholder'),
      },
    },
    {
      fieldName: 'userType',
      label: $t('views.infra.apiAccessLog.userType'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
        allowClear: true,
        placeholder: $t('views.infra.apiAccessLog.userTypePlaceholder'),
      },
    },
    {
      fieldName: 'applicationName',
      label: $t('views.infra.apiAccessLog.applicationName'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.apiAccessLog.applicationNamePlaceholder'),
      },
    },
    {
      fieldName: 'beginTime',
      label: $t('views.infra.apiAccessLog.beginTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'duration',
      label: $t('views.infra.apiAccessLog.duration'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.apiAccessLog.durationPlaceholder'),
      },
    },
    {
      fieldName: 'resultCode',
      label: $t('views.infra.apiAccessLog.resultCode'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.apiAccessLog.resultCodePlaceholder'),
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.infra.apiAccessLog.id'),
      minWidth: 100,
    },
    {
      field: 'userId',
      title: $t('views.infra.apiAccessLog.userId'),
      minWidth: 100,
    },
    {
      field: 'userType',
      title: $t('views.infra.apiAccessLog.userType'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.USER_TYPE },
      },
    },
    {
      field: 'applicationName',
      title: $t('views.infra.apiAccessLog.applicationName'),
      minWidth: 150,
    },
    {
      field: 'requestMethod',
      title: $t('views.infra.apiAccessLog.requestMethod'),
      minWidth: 80,
    },
    {
      field: 'requestUrl',
      title: $t('views.infra.apiAccessLog.requestUrl'),
      minWidth: 300,
    },
    {
      field: 'beginTime',
      title: $t('views.infra.apiAccessLog.beginTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'duration',
      title: $t('views.infra.apiAccessLog.duration'),
      minWidth: 120,
      formatter: ({ cellValue }) => `${cellValue} ms`,
    },
    {
      field: 'resultCode',
      title: $t('views.infra.apiAccessLog.result'),
      minWidth: 150,
      formatter: ({ row }) => {
        return row.resultCode === 0 ? $t('views.infra.apiAccessLog.success') : $t('views.infra.apiAccessLog.fail', [row.resultMsg]);
      },
    },
    {
      field: 'operateModule',
      title: $t('views.infra.apiAccessLog.operateModule'),
      minWidth: 150,
    },
    {
      field: 'operateName',
      title: $t('views.infra.apiAccessLog.operateName'),
      minWidth: 220,
    },
    {
      field: 'operateType',
      title: $t('views.infra.apiAccessLog.operateType'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_OPERATE_TYPE },
      },
    },
    {
      title: $t('common.action'),
      width: 80,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 详情页的字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'id',
      label: $t('views.infra.apiAccessLog.id'),
    },
    {
      field: 'traceId',
      label: $t('views.infra.apiAccessLog.traceId'),
    },
    {
      field: 'applicationName',
      label: $t('views.infra.apiAccessLog.applicationName'),
    },
    {
      field: 'userId',
      label: $t('views.infra.apiAccessLog.userId'),
    },
    {
      field: 'userType',
      label: $t('views.infra.apiAccessLog.userType'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        return h(DictTag, {
          type: DICT_TYPE.USER_TYPE,
          value: data.userType,
        });
      },
    },
    {
      field: 'userIp',
      label: $t('views.infra.apiAccessLog.userIp'),
    },
    {
      field: 'userAgent',
      label: $t('views.infra.apiAccessLog.userAgent'),
    },
    {
      label: $t('views.infra.apiAccessLog.requestInfo'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        if (data?.requestMethod && data?.requestUrl) {
          return `${data.requestMethod} ${data.requestUrl}`;
        }
        return '';
      },
    },
    {
      field: 'requestParams',
      label: $t('views.infra.apiAccessLog.requestParams'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        if (data.requestParams) {
          return h(JsonViewer, {
            value: JSON.parse(data.requestParams),
            previewMode: true,
          });
        }
        return '';
      },
    },
    {
      field: 'responseBody',
      label: $t('views.infra.apiAccessLog.responseBody'),
    },
    {
      label: $t('views.infra.apiAccessLog.beginTime'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        if (data?.beginTime && data?.endTime) {
          return `${formatDateTime(data.beginTime)} ~ ${formatDateTime(data.endTime)}`;
        }
        return '';
      },
    },
    {
      label: $t('views.infra.apiAccessLog.duration'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        return data?.duration ? `${data.duration} ms` : '';
      },
    },
    {
      label: $t('views.infra.apiAccessLog.result'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        if (data?.resultCode === 0) {
          return $t('views.infra.apiAccessLog.success')
	} else if (data && data.resultCode > 0) {
          return $t('views.infra.apiAccessLog.fail', [` | ${data.resultCode} | ${data.resultMsg}`]);
	}
	return '';
      },
    },
    {
      field: 'operateModule',
      label: $t('views.infra.apiAccessLog.operateModule'),
    },
    {
      field: 'operateName',
      label: $t('views.infra.apiAccessLog.operateName'),
    },
    {
      field: 'operateType',
      label: $t('views.infra.apiAccessLog.operateType'),
      content: (data: InfraApiAccessLogApi.ApiAccessLog) => {
        return h(DictTag, {
          type: DICT_TYPE.INFRA_OPERATE_TYPE,
          value: data?.operateType,
        });
      },
    },
  ];
}
